package com.example.rabbitmq.returnlistener;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author weiyx
 * @date 2021-01-10
 */
public class producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1,创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //2, 获取连接
        Connection connection = connectionFactory.newConnection();
        //3, 通过connection创建一个channel
        Channel channel = connection.createChannel();

        String exchangeName = "test_return_exchange";
        String routingKey = "return.save";
        String routingKeyError = "abc.save";

        //5, 发送消息
        String msg = "hello rabbitmq send retuen message!";
        channel.addReturnListener(new ReturnListener() {
            @Override
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey,
                                     AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {
                System.out.println("----- handle return -----");
                System.out.println("replyCode = " + replyCode);
                System.out.println("replyText = " + replyText);
                System.out.println("exchange = " + exchange);
                System.out.println("properties = " + basicProperties);
                System.out.println("msg = " + new String(bytes));


            }
        });
        channel.basicPublish(exchangeName, routingKeyError, true,null, msg.getBytes());

//        channel.close();
//        connection.close();

    }



















}
